Exercise 3.1A

plot_ly(data = HeightData, x = ~Year_18, type = "histogram")

Exercise 3.1B

plot_ly(data = HeightData, x = ~Year_18, color = ~Sex, type = "histogram")
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

Exercise 3.1C

plot_ly(data = HeightData, x = ~Year_18, color = ~Sex, type = "histogram", alpha = 0.6) %>% layout(barmode="overlay")
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

Exercise 3.1D

plot_ly(alpha = 0.6) %>%
  add_histogram(x=HeightData$Year_13) %>%
  add_histogram(x=HeightData$Year_18) %>%
  layout(barmode = "overlay")

Exercise 3.2A

plot_ly(data = HeightData, y = ~Year_18, type = "box", boxmean = T, x0 = "Kids")

Exercise 3.2B

plot_ly(data = HeightData, x = ~Year_18, type = "box", boxmean = T, y0 = "Kids")

Exercise 3.2C

plot_ly(data = HeightData, y = ~Year_18, type = "box", color= ~Sex, boxmean = T)
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

Exercise 3.2D

plot_ly(data = HeightData, y = ~Year_18, type = "box", jitter = 0.6, boxpoints = "all", color= ~Sex, boxmean = T)
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

Exercise 3.3A

HeightData %>%
  plot_ly(y = ~Year_18, type = 'violin', box = list(visible = T), meanline = list(visible = T), x0 = 'Height at 18 (cm)') %>%
  layout(yaxis = list(title = "",zeroline = F))

Exercise 3.3B

HeightData %>%
  plot_ly(x = ~Sex, y = ~Year_18, type = 'violin', box = list(visible = T), split = ~Sex, meanline = list(visible = T), x0 = 'Total Bill') %>%
  layout(xaxis = list(title = "Sex"),
         yaxis = list(title = "Height at 18 (in cm)",zeroline = F))

Exercise 3.3C

df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv")
df %>%
  plot_ly(y = ~total_bill, type = 'violin', box = list(visible = T), meanline = list(visible = T), x0 = 'Total Bill') %>% 
  layout(yaxis = list(title = "", zeroline = F))

Exercise 3.4

Here are the histograms.

# Histogram of every car's CityMPG
h1 <- plot_ly(data = Cars2015, x = ~CityMPG, type = "histogram")
h1
# Histogram of every car's CityMPG separated by the size of the car
h2 <- plot_ly(data = Cars2015, x = ~CityMPG, color = ~Size, type = "histogram")
h2
# Overlayed histogram of every car's CityMPG separated by the size of the car
h3 <- plot_ly(data = Cars2015, x = ~CityMPG, color = ~Size, type = "histogram", alpha = 0.6) %>% layout(barmode="overlay")
h3
# Histogram of every car's CityMPG along with their HwyMPG
h4 <- Cars2015 %>%
  plot_ly(alpha = 0.6) %>%
  add_histogram(x=~CityMPG) %>%
  add_histogram(x=~HwyMPG) %>%
  layout(barmode = "overlay")
h4

Here are the boxplots.

# Boxplot of every car's CityMPG (Vertical)
b1 <- plot_ly(data = Cars2015, y = ~CityMPG, type = "box", boxmean = T, x0 = "Cars")
b1
# Boxplot of every car's CityMPG (Horizontal)
b2 <- plot_ly(data = Cars2015, x = ~CityMPG, type = "box", boxmean = T, y0 = "Cars")
b2
# Boxplot of every car's CityMPG separated by the size of the car
b3 <- plot_ly(data = Cars2015, y = ~CityMPG, type = "box", color= ~Size, boxmean = T)
b3
#  Boxplot of every car's CityMPG separated by the size of the car with all boxpoints shown
b4 <- plot_ly(data = Cars2015, y = ~CityMPG, type = "box", jitter = 0.6, boxpoints = "all", color= ~Size, boxmean = T)
b4

Here are the violin plots.

# Violin plot of every car's CityMPG 
v1 <- Cars2015 %>%
  plot_ly(y = ~CityMPG, type = 'violin', box = list(visible = T), meanline = list(visible = T), x0 = 'City MPG') %>%
  layout(yaxis = list(title = "",zeroline = F))
v1
# Violin plot of every car's CityMPG separated by the size of the car
v2 <- Cars2015 %>%
  plot_ly(x = ~Size, y = ~CityMPG, type = 'violin', box = list(visible = T), split = ~Size, meanline = list(visible = T), 
  x0 = 'Total Bill') %>% 
  layout(xaxis = list(title = "Size of Car"), yaxis = list(title = "City MPG",zeroline = F))
v2
# Violin plot of every car's HwyMPG separated by the size of the car
v3 <- Cars2015 %>%
  plot_ly(x = ~Size, y = ~HwyMPG, type = 'violin', box = list(visible = T),
    split = ~Size, meanline = list(visible = T)) %>% 
  layout(xaxis = list(title = "Size of Car"), yaxis = list(title = "Highway MPG",zeroline = F))
v3

Exercise 3.5

I chose the mtcars dataset and created a 3D scatterplot with x as wt (the weight of the car in 1000 lbs), y as hp (the gross horsepower of the car), and z as qsec (the time it took the car to achieve 1/4 mile).

plot_ly(mtcars, x = ~wt, y = ~hp, z = ~qsec,
        marker = list(color = ~mpg, colorscale = c('#FFE1A1', '#683531'), showscale = TRUE)) %>%
  add_markers() %>%
  layout(scene = list(xaxis = list(title = 'Weight'),
                     yaxis = list(title = 'Gross horsepower'),
                     zaxis = list(title = '1/4 mile time')),
         annotations = list(
           x = 1.13,
           y = 1.05,
           text = 'Miles/(US) gallon',
           xref = 'paper',
           yref = 'paper',
           showarrow = FALSE
         ))